package com.macro.mall.es.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.macro.mall.common.dto.IndexDto;
import com.macro.mall.es.service.EsIndexService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;

@Service
@Slf4j
public class EsIndexServiceImpl implements EsIndexService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;


    @Override
    public void createIndex(IndexDto indexDto) {
        String indexName = indexDto.getIndexName();
        if (StringUtils.isBlank(indexName)){
            throw new RuntimeException("索引名称不能为空");
        }
        boolean indexNameExists = indexExists(indexDto.getIndexName());
        if (indexNameExists){
            throw new RuntimeException("索引已存在："+indexName);
        }
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        createIndexRequest.settings(indexDto.getIndexSettings(), XContentType.JSON);
        createIndexRequest.mapping(indexDto.getIndexMappers(), XContentType.JSON);
        try {
            restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            log.info("索引创建成功："+indexName);
        } catch (IOException e) {
            log.error("索引创建失败，错误信息：{}",e.getMessage());
            throw new RuntimeException("创建索引失败："+indexName,e);
        }
    }

    @Override
    public void updateIndex(IndexDto indexDto) {
        String indexName = indexDto.getIndexName();
        if (StringUtils.isBlank(indexName)){
            throw new RuntimeException("索引名称不能为空");
        }
        boolean indexNameExists = indexExists(indexDto.getIndexName());
        if (!indexNameExists){
            throw new RuntimeException("索引不存在");
        }
        UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexName);
        updateSettingsRequest.settings(indexDto.getIndexSettings(), XContentType.JSON);
        try {
            restHighLevelClient.indices().putSettings(updateSettingsRequest, RequestOptions.DEFAULT);
            log.info("索引修改成功："+indexName);
        } catch (IOException e) {
            log.error("索引修改失败，错误信息：{}",e.getMessage());
            throw new RuntimeException("修改索引失败："+indexName,e);
        }
    }

    @Override
    public void deleteIndex(String indexName) {
        if (StringUtils.isBlank(indexName)){
            throw new RuntimeException("索引名称不能为空");
        }
        if (!indexExists(indexName)){
            throw new RuntimeException("索引不存在");
        }
        try {
            restHighLevelClient.indices().delete(new DeleteIndexRequest(indexName), RequestOptions.DEFAULT);
            log.info("删除成功："+indexName);
        } catch (IOException e) {
            log.error("删除失败：{},",e.getMessage());
            throw new RuntimeException("删除失败："+indexName,e);
        }
    }

    /**
     * 判断索引是否存在
     * @param indexName
     * @return
     */
    public boolean indexExists(String indexName) {
        if (StringUtils.isBlank(indexName)){
            return false;
        }
        try {
            //创建请求对象
            GetIndexRequest request = new GetIndexRequest(indexName);
            //执行请求
            return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("异常信息：{}",e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}
